import request from '@/utils/axios'
export function queryBlogById(data: {id: number, userId?: number}): Promise<any> {
  return request({
    url: `/blog/getBlogById/${data.id}` + (data?.userId? `?userId=${data.userId}` : ''),
    method: 'get'
  })
}

export function insertBlog(data: any): Promise<any> {
  return request({
    url: '/blog/createBlog',
    method: 'post',
    data
  })
}

export function removeBlog(id: number): Promise<any> {
  return request({
    url: '/blog/delete/' + id,
    method: 'delete',
  })
}

export function updateBlog(data: any): Promise<any> {
  return request({
    url: '/blog/updateblog',
    method: 'post',
    data
  })
}

export function queryBlogList(page: number, size: number): Promise<any> {
  return request({
    url: `/blog/queryBlogList/?page=${page}&size=${size}`,
    method: 'get'
  })
}

export function titleExist(title: string): Promise<any> {
  return request({
    url: '/blog/titleExist/' + title,
    method: 'get'
  })
}

// 博客分类列表
export function queryBlogCategoryList(): Promise<any> {
  return request({
    url: '/blogCategory/list',
    method: 'get'
  })
}

// 博客标签列表
export function queryBlogTagList(): Promise<any> {
  return request({
    url: '/blogTag/list',
    method: 'get'
  })
}

// 收藏
export function star(id: number): Promise<any> {
  return request({
    url: `/blog/star/${id}`,
    method: 'get'
  })
}

// 取消收藏
export function unstar(id: number): Promise<any> {
  return request({
    url: `/blog/unstar/${id}`,
    method: 'get'
  })
}

// 点赞
export function like(id: number): Promise<any> {
  return request({
    url: `/blog/like/${id}`,
    method: 'get'
  })
}

// 取消点赞
export function unlike(id: number): Promise<any> {
  return request({
    url: `/blog/unlike/${id}`,
    method: 'get'
  })
}

// 阅读次数
export function read(id: number): Promise<any> {
  return request({
    url: `/blog/read/${id}`,
    method: 'get'
  })
}

// 是否已经收藏
export function isStared(id: number): Promise<any> {
  return request({
    url: `/star/${id}`,
    method: 'get'
  })
}

// 阅读次数
export function addComment(id: number): Promise<any> {
  return request({
    url: `/blog/read/${id}`,
    method: 'get'
  })
}

// 阅读次数
export function frontGetCommentTotal(id: number): Promise<any> {
  return request({
    url: `/blog/read/${id}`,
    method: 'get'
  })
}

// 阅读次数
export function frontGetChildrenComment(id: number): Promise<any> {
  return request({
    url: `/blog/read/${id}`,
    method: 'get'
  })
}

// 评论
export function createBlogComment(data:any): Promise<any> {
  return request({
    url: '/blog/comment',
    method: 'post',
    data
  })
}

// 删评论
export function deleteBlogComment(data:{commentId: number, blogId:number}): Promise<any> {
  return request({
    url: `/blog/deleteComment/?commentId=${data.commentId}&blogId=${data.blogId}`,
    method: 'delete'
  })
}

// 聚合消息

export function getToutiaoList(): Promise<any> {
  return request({
    url: '/apis/getToutiaoList',
    method: 'get'
  })
}

export function getHistoryList(): Promise<any> {
  return request({
    url: '/apis/getHistoryList',
    method: 'get'
  })
}

export function getHistoryDetail(id: number): Promise<any> {
  return request({
    url: '/apis/getHistoryDetail/' + id,
    method: 'get'
  })
}

export function getToutiaoDetail(id: string): Promise<any> {
  return request({
    url: '/apis/getToutiaoDetail/' + id,
    method: 'get'
  })
}

export function getRelationArticle(id: number): Promise<any> {
  return request({
    url: '/blog/getRelationArticle/' + id,
    method: 'get'
  })
}

export function queryStarBlogList(page: number, size: number): Promise<any> {
  return request({
    url: '/blog/queryStarBlogList'  + `?page=${page}&size=${size}`,
    method: 'get'
  })
}

export function queryLikeBlogList(page: number, size: number): Promise<any> {
  return request({
    url: '/blog/queryLikeBlogList' + `?page=${page}&size=${size}`,
    method: 'get'
  })
}
type Data = {
  page?: number | undefined;
  size?: number | undefined;
  title?: string | undefined;
  categoryId?: number | undefined;
  tagId?: number | undefined;
  tagName?: string | undefined;
  categoryName?: string | undefined;
  userId?: number | undefined;
  authorName?: string | undefined;
  [key: string]: any; // 添加字符串索引签名
};

export function queryBlogListByParam(data: Data): Promise<any> {
  let query = ''
  for (const key in data) {
    const value = data[key]
    if (value) {
      query += query ? `&${key}=${value}` : `${key}=${value}`;
    }
  }
  return request({
    url: '/blog/queryBlogListByParam?' + query,
    method: 'get'
  })
}